<?php
defined('IN_MAIN') or exit('Invalid Access!');

function calc_group_sales($uid, $date_from, $date_to, $which_field, $first_sale = 'all'){
/*
This function will get total group sales of input uid (user ID) according to which network in the which_field.
*/

	global $vars;
	$db=$vars["db"];
	
	$calc_date['start'] = date("Y-m-d H:i:s", strtotime($date_from));
	$calc_date['end'] = date("Y-m-d H:i:s", strtotime($date_to));
	
	$upline_id = $uid;
	$total_sales = @mysql_result(mysql_query($s="select sum(s.bv) from $db->purchase a inner join $db->sales_bv s on s.slid=a.id inner join $db->users b on b.id=s.uid inner join $db->users c on c.matrix_left<b.matrix_left and c.matrix_right>=b.matrix_left and c.id=".$upline_id.($first_sale != 'all'? " and a.first_sale='".($first_sale? 'y' : 'n')."'" : '')." and a.no_bv!='y' and a.status='confirmed' and a.collected_date>='$calc_date[start]' and a.collected_date<='$calc_date[end]'"), 0) or 0;		
	if(!$total_sales){
		$total_sales = 0;
	}
	
	return $total_sales;
}
?>